export default class CalculateSizeorAltitude {
  /// <summary>
  /// 地球半径常量
  /// </summary>
  static EARTHRADIUS = 6378137;

  /// <summary>
  /// 根据给定的地图范围计算场景的高度
  /// </summary>
  /// <param name="bounds">地图范围</param>
  /// <returns>场景高度</returns>
  static CalculateAltitudeFromBounds(width) {
    let altitude = this.EARTHRADIUS;
    if (width >= 120) {
    let mapWidth = width;
    altitude = this.EARTHRADIUS * mapWidth / 60 - this.EARTHRADIUS;
  } else if (width != 0) {
    let angle1 = width / 2 / 180 * 3.1415926;
    let height = Math.sin(angle1) * this.EARTHRADIUS;
    let a = height / Math.tan(angle1);
    let b = height / Math.tan(3.1415926 / 6);
    altitude = a + b - this.EARTHRADIUS;
  }
  return altitude;
}

/// <summary>
/// 根据给定的场景高度计算地图中显示范围的宽度
/// </summary>
/// <param name="altitude">场景高度</param>
/// <returns>地图显示范围尺寸</returns>
static CalculateSizeFromAltitude(altitude)
{
  let size = {};
    //当场景高度大于可全幅显示整球的高度时
    if (altitude >= this.EARTHRADIUS) {
      let ratio = (altitude + this.EARTHRADIUS) / 2;
      let longitudeWidth = 120 * ratio / this.EARTHRADIUS;
      let latitudeWidth = 120 * ratio / this.EARTHRADIUS;
      size.width=longitudeWidth;
      size.height=latitudeWidth;
    } else {//当场景高度小于可全幅显示整球的高度时，即无法看到整球时
      let tan30 = Math.tan(3.1415926 / 6);
      //设置二元一次方程组的a、b、c值
      let a = (Math.pow(tan30, 2) + 1) * Math.pow(this.EARTHRADIUS, 2);
      let b = -2 * (this.EARTHRADIUS + altitude) * this.EARTHRADIUS * Math.Pow(tan30, 2);
      let c = Math.pow(tan30, 2) * Math.pow((this.EARTHRADIUS + altitude), 2) - Math.pow(this.EARTHRADIUS, 2.0);
      //解一元二次方程，取锐角，因此余弦值较大
      let cosd = (-b + Math.sqrt(Math.Pow(b, 2) - 4 * a * c)) / 2 / a;
      let d = Math.acos(cosd);
      let widthd = 2 * d * this.EARTHRADIUS;
      let width = (widthd / 3.1415926 / this.EARTHRADIUS) * 180;
      size.width=width;
      size.height=width;
    }
  return size;
  }
}
